package com.elh.jpa.service;

import com.elh.jpa.dao.base.BaseRepository;
import org.springframework.data.domain.*;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * @author liulei
 **/
public abstract class BaseServiceImpl<T extends Serializable,ID> implements IBaseService<T,ID> {

    public abstract BaseRepository<T,ID> getBaseRepository();

    @Override
    public void deleteInBatch(Iterable<T> entities) {
        getBaseRepository().deleteInBatch(entities);
    }

    @Override
    public void deleteAllInBatch() {
        getBaseRepository().deleteAllInBatch();
    }

    @Override
    public void deleteById(ID id) {
        getBaseRepository().deleteById(id);
    }

    @Override
    public void delete(T entity) {
        getBaseRepository().delete(entity);
    }

    @Override
    public T getOne(ID id) {
        return getBaseRepository().getOne(id);
    }

    @Override
    public Optional<T> findOne(T t) {
        return getBaseRepository().findOne(Example.of(t));
    }

//    @Override
//    public Optional<T> findOne(Specification<T> spec) {
//        return getBaseRepository().findOne(spec);
//    }

//    @Override
//    public <S extends T> Optional<S> findOne(Example<S> example) {
//        return getBaseRepository().findOne(example);
//    }

    @Override
    public List<T> findAllById(Iterable<ID> ids) {
        return getBaseRepository().findAllById(ids);
    }

    @Override
    public Optional<T> findById(ID id) {
        return getBaseRepository().findById(id);
    }

    @Override
    public boolean existsById(ID id) {
        return getBaseRepository().existsById(id);
    }

    @Override
    public <S extends T> boolean exists(T t) {
        return getBaseRepository().exists(Example.of(t));
    }

//    @Override
//    public <S extends T> boolean exists(Example<S> example) {
//        return getBaseRepository().exists(example);
//    }

    @Override
    public List<T> findAll() {
        return getBaseRepository().findAll();
    }

    @Override
    public Iterable<T> findAll(Sort sort) {
        return getBaseRepository().findAll(sort);
    }

    @Override
    public Page<T> findAllByPage(int page, int size) {
        return getBaseRepository().findAll(PageRequest.of(page, size));
    }


    @Override
    public Page<T> findAllByPage(int page, int size, Sort sort) {
        return getBaseRepository().findAll(PageRequest.of(page, size,sort));
    }

    @Override
    public Page<T> findAllByPage(int page, int size, T t, Sort sort) {
        Example<T> example = Example.of(t);
        PageRequest pageRequest = PageRequest.of(page, size, sort);
        return getBaseRepository().findAll(example,pageRequest);
    }


    @Override
    public long count(T t) {
        return  getBaseRepository().count(Example.of(t));
    }

//    @Override
//    public List<T> findAll(Specification<T> spec) {
//        return getBaseRepository().findAll(spec);
//    }
//
//    @Override
//    public List<T> findAll(Specification<T> spec, Sort sort) {
//        return getBaseRepository().findAll(spec,sort);
//    }
//
//    @Override
//    public Page<T> findAll(Specification<T> spec, Pageable pageable) {
//        return getBaseRepository().findAll(spec,pageable);
//    }
//
//    @Override
//    public <S extends T> List<S> findAll(Example<S> example) {
//        return getBaseRepository().findAll(example);
//    }

//    @Override
//    public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
//        return getBaseRepository().findAll(example,sort);
//    }
//
//    @Override
//    public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
//        return getBaseRepository().findAll(example,pageable);
//    }
//
//    @Override
//    public <S extends T> long count(Example<S> example) {
//        return getBaseRepository().count(example);
//    }
//
//    @Override
//    public long count(Specification<T> spec) {
//        return getBaseRepository().count(spec);
//    }

    @Override
    public long count() {
        return getBaseRepository().count();
    }

    @Override
    public <S extends T> S save(S entity) {
        return getBaseRepository().save(entity);
    }

    @Override
    public <S extends T> List<S> saveAll(Iterable<S> entities) {
        return getBaseRepository().saveAll(entities);
    }

}
